package cn.cjxy.controller;

import cn.cjxy.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
@Controller
public class HelloSpringbootController {
    @ResponseBody//返回json格式数据并跳出视图解析器
    @RequestMapping(method = RequestMethod.GET,path = "/hello")
    public String hello() {
        return "Hello Springboot!";
    }

    /**
     * 请求对象接收参数
     * @param request 请求对象
     * @return 响应结果
     */
    @ResponseBody
    @RequestMapping("/requestParam")
    public String requestParam(HttpServletRequest request) {
      //请求地址   http://localhost:8081/cjxy/requestParam?name=zhangsan&age=18
        //通过参数名匹配得到参数值
        String name = request.getParameter("name");
        String age = request.getParameter("age");

        System.out.println("姓名是："+name+"---"+"年龄是： "+age);
        return "请求成功";
    }

    /**
     * 形参接收简单参数
     * @param name 姓名
     * @param age  年龄
     * @return
     */
    @ResponseBody
    @RequestMapping("/simpleParam")
    //@RequestParam 将前端的参数名称对应到后端接收参数的名称
    public String simpleParam(@RequestParam(value = "name1",required = false) String name, Integer  age) {
        //请求地址   http://localhost:8081/cjxy/requestParam
        System.out.println("姓名是："+name+"---"+"年龄是： "+age);
        return "请求成功";
    }

    /**
     * 形参接收对象参数
     * @param user  user  对象
     * @return
     */
    @ResponseBody
    @RequestMapping("/domainParam")
    public String domainParam(User user) {
       // http://localhost:8081/cjxy/domainParam?name=zhangsan&age=18
        System.out.println(user);
        return "请求成功";
    }

    /**
     * 形参接收对象参数-复杂
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("/complexParam")
    public String complexParam(User user) {
        // http://localhost:8081/cjxy/complexParam?name=zhangsan&age=18&address.province=四川省&address.city=成都市
        System.out.println(user);
        return "请求成功";
    }

    /**
     * 形参接收组合参数-数组
     * @param musicType 音乐类型
     * @return
     */
    @ResponseBody
    @RequestMapping("/arrayParam")
    public String arrayParam(String[] musicType) {
        System.out.println(Arrays.toString(musicType));
        return "请求成功";
    }


    /**
     * 形参接收组合参数-集合
     * @param musicType 音乐类型
     * @return
     */
    @ResponseBody
    @RequestMapping("/listParam")
    public String listParam(@RequestParam List<String> musicType) {
        System.out.println(musicType);
        return "请求成功";
    }

    /**
     * 形参接收-日期
     * @param birthday
     * @return
     */
    @ResponseBody
    @RequestMapping("/dataParam")//2025-06-25 10:05:59
    public String dataParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime birthday) {
        //  请求地址  http://localhost:8081/cjxy/dataParam?birthday=2025-06-25 10:05:59
        System.out.println(birthday);
        return "请求成功！";
    }

    /**
     * 形参接收-JSON参数
      * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "/jsonParam")
    public String jsonParam(@RequestBody User user) {
    //请求地址 http://localhost:8081/cjxy/jsonParam
        System.out.println(user);
        return "请求成功！";
    }

    /**
     * 形参接收路径参数-单个
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/user/{id}")//前端传递的参数名也叫id
    public String user(@PathVariable("id") Integer id) {
        //请求地址 http://localhost:8081/cjxy/user/1
        System.out.println(id);
        return "请求成功";
    }
    /**
     * 形参接收路径参数-多个
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/user/{id}/{name}")//前端传递的参数名也叫id
    public String user(@PathVariable("id") Integer id,@PathVariable("name")String name) {
        //请求地址 http://localhost:8081/cjxy/user/1/zhangjie
        System.out.println(id+"---"+name);
        return "请求成功";
    }


}
